/**
 * 
 */
package mblog.web.controller.front;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import mblog.core.planet.PostPlanet;
import mblog.core.pojos.Product;
import mblog.core.pojos.Tag;
import mblog.web.controller.BaseController;
import mtons.modules.pojos.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.faxsun.core.persist.entity.BannerPO;
import com.faxsun.core.pojos.enums.TagType;
import com.faxsun.core.service.BannerService;
import com.faxsun.core.service.IndexService;
import com.faxsun.core.service.TagService;
import com.faxsun.web.controller.FrontViewConstants;
import com.faxsun.web.utils.MobileDetect;

/**
 * @author langhsu
 *
 */
@Controller(value = "indexController")
public class IndexController extends BaseController {
	@Autowired
	private PostPlanet postPlanet;

	@Autowired
	private IndexService indexService;

	@Autowired
	private TagService tagService;

	@Autowired
	private BannerService bannerService;

	private static final int generalShowSize = 6;

	@RequestMapping("/")
	public String root(HttpServletRequest request, Integer pn, ModelMap model) {
		// FIXME: all constants can be configurable
		Page page = wrapPage(pn);
		page = postPlanet.paging(page);
		model.put("page", page);

		// FIXME:将每个模块的个数，放入到settingpo中
		// 将第一个商品单独取出，放置到大图，将最后四个商品放到分类板块
		Page recommendPage = wrapPage(1);
		recommendPage.setMaxResults(9);
		indexService.retrieveRecommendProducts(recommendPage);

		@SuppressWarnings("unchecked")
		List<Product> recommendProducts = (List<Product>) recommendPage.getResults();
		int productsSize = recommendProducts.size();

		Product largeProduct = null;
		List<Product> categoryProduct = new ArrayList<Product>();

		if (productsSize > 0) {
			largeProduct = recommendProducts.get(0);

			if (productsSize >= generalShowSize) {
				for (int i = generalShowSize; i >= 1; i--) {
					categoryProduct.add(recommendProducts.get(productsSize - i));
				}
			} else {
				// FIXME:补空白策略
				categoryProduct.addAll(recommendProducts);
			}
			// remove separated elements
			// FIXME:简化移除操作代码
			/// *
			if (productsSize > 1) {
				recommendProducts.remove(0);
			}
			if (productsSize > 4) {
				recommendProducts.remove(recommendProducts.size() - 1);
				recommendProducts.remove(recommendProducts.size() - 1);
				recommendProducts.remove(recommendProducts.size() - 1);
				recommendProducts.remove(recommendProducts.size() - 1);
			}
			// */
		}
		Page hotEvaluationPage = wrapPage(1);
		hotEvaluationPage.setMaxResults(5);
		indexService.retrieveHotEvluation(hotEvaluationPage);

		Page hotInfoPage = wrapPage(1);
		hotInfoPage.setMaxResults(generalShowSize);
		indexService.retrieveHotInfo(hotInfoPage);

		Page newestCommentPage = wrapPage(1);
		newestCommentPage.setMaxResults(generalShowSize);
		indexService.retrieveNewestComment(newestCommentPage);

		model.addAttribute("largeProduct", largeProduct);
		model.addAttribute("productList", recommendProducts);
		model.addAttribute("categoryProduct", categoryProduct);
		model.addAttribute("hotEvaluationList", hotEvaluationPage.getResults());
		model.addAttribute("hotInfoList", hotInfoPage.getResults());
		model.addAttribute("newestCommentList", newestCommentPage.getResults());

		// 增加banner
		List<BannerPO> banners = bannerService.listDisplayedBanners();
		model.addAttribute("banners", banners);

		// 分类评测导航
		// FIXME:5 is configurable
		List<Tag> navTags = tagService.listNavTags(TagType.PRODUCT.getValue());
		if (navTags.size() > generalShowSize) {
			navTags = navTags.subList(0, generalShowSize);
		}
		model.addAttribute("navTags", navTags);
		// FIXME: generalShowSize is configurable
		model.addAttribute("navTagsProductMap", indexService.retrieveTopProductsByCategory(navTags, generalShowSize));
		// TKD
		model.addAttribute(FrontViewConstants.VIEW_META_TITLE, "范客生活-首页");
		// TODO:add tdk
		model.addAttribute(FrontViewConstants.VIEW_META_KEYWORD, "范客生活，范客资讯，智能硬件社区，海淘社区，智能硬件资讯");
		model.addAttribute(FrontViewConstants.VIEW_META_DESC,
				"范客生活是一家致力于改变您生活品质的智能硬件海淘社区！现在加入范客生活，关注最新最好玩的智能生活产品，并且有多种多样实惠精选的产品等你带回家！全场保证原产地正品，海外产品直邮，国内产品低价实惠，全场免邮费！专业的智能硬件团队，我在范客生活等你！");
		// return getView(Views.INDEX);
		String viewUrl = MobileDetect.getProperView(request, "/views/mobile/index", "/views/home/index");
		return viewUrl;
	}

	// @RequestMapping("/index")
	// public String index(Integer pn, ModelMap model) {
	// Page page = wrapPage(pn);
	// page = postPlanet.paging(page);
	// model.put("page", page);
	// return getView(Views.INDEX);
	// }
}
